package com.uls.web.controller.ycl;

import java.util.Date;
import java.util.List;
import javax.servlet.http.HttpServletResponse;

import com.uls.ycl.domain.RepairMaterial;
import com.uls.ycl.mapper.RepairMaterialMapper;
import com.uls.ycl.service.IRepairMaterialService;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.uls.common.annotation.Log;
import com.uls.common.core.controller.BaseController;
import com.uls.common.core.domain.AjaxResult;
import com.uls.common.enums.BusinessType;
import com.uls.ycl.domain.RepairItem;
import com.uls.ycl.service.IRepairItemService;
import com.uls.common.utils.poi.ExcelUtil;
import com.uls.common.core.page.TableDataInfo;

/**
 * 报修项目Controller
 * 
 * @author ycl
 * @date 2024-11-18
 */
@RestController
@RequestMapping("/ycl/item")
public class RepairItemController extends BaseController
{
    @Autowired
    private IRepairItemService repairItemService;

    @Autowired
    private IRepairMaterialService repairMaterialService;
        @Autowired
    private RepairMaterialMapper repairMaterialMapper;
    /**
     * 查询报修项目列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:item:list')")
    @GetMapping("/list")
    public TableDataInfo list(RepairItem repairItem)
    {
        startPage();
        List<RepairItem> list = repairItemService.selectRepairItemList(repairItem);

        for (RepairItem item:list){
            Long materialId = item.getMaterialId();
            RepairMaterial repairMaterial = repairMaterialService.selectRepairMaterialByMaterialId(materialId);
            if(repairMaterial!=null){
                item.setMname(repairMaterial.getName());
            }

        }

        return getDataTable(list);
    }

    /**
     * 导出报修项目列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:item:export')")
    @Log(title = "报修项目", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, RepairItem repairItem)
    {
        List<RepairItem> list = repairItemService.selectRepairItemList(repairItem);
        ExcelUtil<RepairItem> util = new ExcelUtil<RepairItem>(RepairItem.class);
        util.exportExcel(response, list, "报修项目数据");
    }

    /**
     * 获取报修项目详细信息
     */
    @PreAuthorize("@ss.hasPermi('ycl:item:query')")
    @GetMapping(value = "/{itemId}")
    public AjaxResult getInfo(@PathVariable("itemId") Long itemId)
    {
        return success(repairItemService.selectRepairItemByItemId(itemId));
    }

    /**
     * 新增报修项目
     */
    @PreAuthorize("@ss.hasPermi('ycl:item:add')")
    @Log(title = "报修项目", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody RepairItem repairItem)
    {
        repairItem.setUserName(getUsername());
        repairItem.setCreatedAt(new Date());
        RepairMaterial repairMaterial =repairMaterialMapper.selectRepairMaterialByMaterialId(repairItem.getMaterialId());
        if(repairMaterial.getTotal()<repairItem.getTotal()){
            return AjaxResult.warn("库存不足");
        }
        return toAjax(repairItemService.insertRepairItem(repairItem));
    }

    /**
     * 修改报修项目
     */
    @PreAuthorize("@ss.hasPermi('ycl:item:edit')")
    @Log(title = "报修项目", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody RepairItem repairItem)
    {
        repairItem.setUpdateAt(new Date());
        RepairMaterial repairMaterial =repairMaterialMapper.selectRepairMaterialByMaterialId(repairItem.getMaterialId());
        if(repairMaterial.getTotal()<repairItem.getTotal()){
            return AjaxResult.warn("库存不足");
        }
        return toAjax(repairItemService.updateRepairItem(repairItem));
    }

    /**
     * 删除报修项目
     */
    @PreAuthorize("@ss.hasPermi('ycl:item:remove')")
    @Log(title = "报修项目", businessType = BusinessType.DELETE)
	@DeleteMapping("/{itemIds}")
    public AjaxResult remove(@PathVariable Long[] itemIds)
    {
        return toAjax(repairItemService.deleteRepairItemByItemIds(itemIds));
    }



    /**
     * 提交项目信息申请
     */
    @PreAuthorize("@ss.hasPermi('ycl:item:itemSubmit')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @GetMapping("/submitItem/{itemId}")
    public AjaxResult submitStudent(@PathVariable Long itemId)
    {
        return  toAjax(repairItemService.submit(itemId,getUserId()));
    }

    /**
     * 查询未审批项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:item:listNo')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @GetMapping("/listNo")
    public TableDataInfo listNo(RepairItem item)
    {
        startPage();
         //List<SysStudent> list = sysStudentService.noFinishTask(getUserId(),"myStudentProcess");//关联工作流没有带条件查询
        List<RepairItem> list = repairItemService.noFinishTask(getUserId(),"myitem",item);//关联工作流带条件查询

        for (RepairItem item1:list){
            Long materialId = item1.getMaterialId();
            RepairMaterial repairMaterial = repairMaterialService.selectRepairMaterialByMaterialId(materialId);
            if(repairMaterial!=null){
                item1.setMname(repairMaterial.getName());
            }

        }

        return getDataTable(list);

    }




    /*获取项目详细信息*/
    @GetMapping("/getItemComment/{itemId}")
    public AjaxResult getItemInfo(@PathVariable("itemId") Long itemId)
    {
        RepairItem item = repairItemService.selectRepairItemByItemId(itemId);

        item.setTaskCommentVoList(repairItemService.getTaskCommentList(item.getFlowInstantId()));  //找出前面审批人的审批信息
        return AjaxResult.success(item);
    }


    /*查询未审批的项目列表，审批*/
    @PreAuthorize("@ss.hasPermi('ycl:item:editnoapprove')")
    @Log(title = "查询未审批的项目信息列表", businessType = BusinessType.EXPORT)
    @PostMapping("/editnoapprove")
    public AjaxResult editStudentNoApprove(@RequestBody RepairItem item){
        repairItemService.completeTask(item.getFlowInstantId(),item.getAgree(),getUserId(),item.getComment(),item.getItemId());
        return toAjax(200);
    }




    /**
     * 查询已审批项目信息列表
     */
    @PreAuthorize("@ss.hasPermi('ycl:item:listFinished')")
    @Log(title = "项目信息", businessType = BusinessType.EXPORT)
    @GetMapping("/listFinished")
    public TableDataInfo listFinished(RepairItem item)
    {




        startPage();
        //List<SysStudent> list = sysStudentService.noFinishTask(getUserId(),"myStudentProcess");//关联工作流没有带条件查询
        List<RepairItem> list = repairItemService.finishedTask(getUserId(),"myitem",item);//关联工作流带条件查询

                for (RepairItem item1:list){
            Long materialId = item1.getMaterialId();
            RepairMaterial repairMaterial = repairMaterialService.selectRepairMaterialByMaterialId(materialId);
            if(repairMaterial!=null){
                item1.setMname(repairMaterial.getName());
            }

        }

        return getDataTable(list);


    }



}
